package com.jindy.module.warningOrderView.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

import com.jindy.common.core.domain.AjaxResult;
import com.jindy.module.homepage.service.HomepageService;
import com.jindy.module.homepage.vo.SimpleKanbanInConditionVO;
import com.jindy.module.homepage.vo.SimpleKanbanInfoVO;
import com.jindy.module.order.domain.Order;
import com.jindy.module.order.service.IOrderService;
import com.jindy.module.warningOrderView.domain.WarningOrderViewVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.jindy.module.warningOrderView.mapper.WarningOrderViewMapper;
import com.jindy.module.warningOrderView.domain.WarningOrderView;
import com.jindy.module.warningOrderView.service.IWarningOrderViewService;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;

import static com.jindy.common.utils.PageUtils.startPage;

/**
 * VIEWService业务层处理
 * 
 * @author jindy
 * @date 2023-06-13
 */
@Service
public class WarningOrderViewServiceImpl implements IWarningOrderViewService 
{
    @Autowired
    private WarningOrderViewMapper warningOrderViewMapper;

    @Resource
    private HomepageService homepageService;

    @Resource
    private IOrderService iOrderService;

    /**
     * 查询VIEW
     * 
     * @param salesmanName VIEW主键
     * @return VIEW
     */
    @Override
    public WarningOrderView selectWarningOrderViewBySalesmanName(String salesmanName)
    {
        return warningOrderViewMapper.selectWarningOrderViewBySalesmanName(salesmanName);
    }

    /**
     * 查询VIEW列表
     * 
     * @param simpleKanbanInConditionVO VIEW
     * @return VIEW
     */
    @Override
    public List<WarningOrderViewVO> selectWarningOrderViewList(SimpleKanbanInConditionVO simpleKanbanInConditionVO)
    {
        List<WarningOrderViewVO> warningOrderList = new ArrayList<>();
        AjaxResult result = homepageService.selectSimpleKanbanInfo(simpleKanbanInConditionVO);
        if (Objects.isNull(result)){
            return null;
        }
        Integer code = (Integer) result.get("code");
        if (200 != code){
            return null;
        }
        SimpleKanbanInfoVO data = (SimpleKanbanInfoVO)result.get("data");
        List<String> orderNoList = data.getWarningOrder();
        if (!CollectionUtils.isEmpty(orderNoList)){
            startPage();
            List<Order> orderList = iOrderService.findOrdersByNumbers(orderNoList);
            for (Order order : orderList) {
                WarningOrderViewVO warningOrderViewVO = new WarningOrderViewVO();
                BeanUtils.copyProperties(order,warningOrderViewVO);
                warningOrderList.add(warningOrderViewVO);
            }
        }
        return warningOrderList;
    }

    /**
     * 新增VIEW
     * 
     * @param warningOrderView VIEW
     * @return 结果
     */
    @Override
    public int insertWarningOrderView(WarningOrderView warningOrderView)
    {
        return warningOrderViewMapper.insertWarningOrderView(warningOrderView);
    }

    /**
     * 修改VIEW
     * 
     * @param warningOrderView VIEW
     * @return 结果
     */
    @Override
    public int updateWarningOrderView(WarningOrderView warningOrderView)
    {
        return warningOrderViewMapper.updateWarningOrderView(warningOrderView);
    }

    /**
     * 批量删除VIEW
     * 
     * @param salesmanNames 需要删除的VIEW主键
     * @return 结果
     */
    @Override
    public int deleteWarningOrderViewBySalesmanNames(String[] salesmanNames)
    {
        return warningOrderViewMapper.deleteWarningOrderViewBySalesmanNames(salesmanNames);
    }

    /**
     * 删除VIEW信息
     * 
     * @param salesmanName VIEW主键
     * @return 结果
     */
    @Override
    public int deleteWarningOrderViewBySalesmanName(String salesmanName)
    {
        return warningOrderViewMapper.deleteWarningOrderViewBySalesmanName(salesmanName);
    }
}
